eststo clear

if("`c(os)'"=="Windows"){
	local tableSaveDir = "ReviewResponseResults\"
	local plotSaveDir = "ReviewResponseResults\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "ReviewResponseResults/"
	local plotSaveDir = "ReviewResponseResults/"
	local pythonSaveDir = "PythonScripts/"
}

//Establish Control Variable Sets
//Spec1 controls are the same for both registrations and fuel use 
local spec1Controls = "c.normYear#i.stateGroup c.quadYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"

//Other controls are not the same for both registrations and VMT 
//Registration Controls
local spec2RegControls = "`spec1Controls' i.transactionDataDummy"
local spec3RegControls = "`spec2RegControls' i.regDummyExtSpec3"
local spec4RegControls = "`spec2RegControls' i.regDummyExtSpec4"


di "`spec3RegControls'"

//Gas use Controls
local spec2GasUseControls = "`spec1Controls'"
local spec3GasUseControls = "`spec2GasUseControls' i.gasUseDummyExtSpec3"
local spec4GasUseControls = "`spec2GasUseControls' i.gasUseDummyExtSpec4"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Establish Cluster Variable
local clusterVar = "stateGroup"

//Establish primary null
local primaryNull = 11
local altNull = round(9.7, .1)
local altNull2 = 1
local altNull3 = -4.8
local altNull4 = -6.1

frame create emissions
frame change emissions

if("`c(os)'"=="Windows"){
	local dataFile = "CleanedData\EmissionsProgramsV2.xlsx"
}
else{
	local dataFile = "CleanedData/EmissionsProgramsV2.xlsx"
}

import excel using "`dataFile'", first sheet("Clean") case(l)

frame change default
frlink m:1 state, frame(emissions)
frget stateemissionsprogram startemission endemission, from(emissions)

gen emissionind = 0
replace emissionind = 1 if year>=startemission & year<=endemission & stateemissionsprogram==1

frame drop emissions

//non-normalized models
local treatmentVar = "1.nosafetyind"

local fsVarsSpec2 = "logregistrations i.nosafetyind `spec2RegControls' i.emissionind"
local rfVarsSpec2 = "loghighwaygasuse i.nosafetyind `spec2GasUseControls' i.emissionind"

local fsOut = "logregistrations"
local rfOut = "loghighwaygasuse"

local treatControl = "nosafetyind"

local fsControlsSpec1 = "`spec1Controls' i.emissionind"
local rfControlsSpec1 = "`spec1Controls' i.emissionind"

local modelTitleSpec1 = "Spec1GasUseEmissions"
local nullLowSpec1 = -4
local nullDeltaSpec1 = 0.001
local nullHighSpec1 = 1

runModelFWL `fsOut', `rfOut', `fsControlsSpec1', `rfControlsSpec1', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec1', `nullLowSpec1', `nullDeltaSpec1', `nullHighSpec1', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

local fsControlsSpec2 = "`spec2RegControls' i.emissionind"
local rfControlsSpec2 = "`spec2GasUseControls' i.emissionind"

local modelTitleSpec2 = "Spec2GasUseEmissions"
local nullLowSpec2 = -4
local nullDeltaSpec2 = 0.001
local nullHighSpec2 = 1

runModelFWL `fsOut', `rfOut', `fsControlsSpec2', `rfControlsSpec2', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec2', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
// runModel `fsVarsSpec2', `rfVarsSpec2', `clusterVar', `treatmentVar', `modelTitleSpec2', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
//
//
// // //Combine all of the IV information
// // python script "`pythonSaveDir'combineIV.py"
// //
// // //Output tables
// // constructTable GasUsePerCapitaiv; Fleet Gas Use Per Capita; Spec1GasUsePerCapita Spec2GasUsePerCapita Spec3GasUsePerCapita Spec4GasUsePerCapita; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
// // constructTable GasUseiv; Fleet Gas Use; Spec1GasUse Spec2GasUse Spec3GasUse Spec4GasUse; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
